package com.ruoyi.outbound.outbound.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import com.ruoyi.basicData.product.domain.Product;
import com.ruoyi.basicData.product.mapper.ProductMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.outbound.consignment.domain.Consignment;
import com.ruoyi.outbound.consignment.mapper.ConsignmentMapper;
import com.ruoyi.outbound.consignment.service.impl.ConsignmentServiceImpl;
import com.ruoyi.outbound.outbound.domain.BicycleOut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.outbound.outbound.mapper.OutboundMapper;
import com.ruoyi.outbound.outbound.domain.Outbound;
import com.ruoyi.outbound.outbound.service.IOutboundService;

/**
 * 出库单Service业务层处理
 * 
 * @author zzx
 * @date 2024-02-21
 */
@Service
public class OutboundServiceImpl implements IOutboundService
{
    @Autowired
    private OutboundMapper outboundMapper;
    @Autowired
    private ConsignmentMapper consignmentMapper;
    @Autowired
    private ProductMapper productMapper;


    /**
     * 查询出库单
     *
     * @param containerNumber 出库单主键
     * @return 出库单
     */
    @Override
    public Outbound selectOutboundByConsignmentId(String containerNumber)
    {
        Outbound outbound = outboundMapper.selectOutboundByConsignmentId(containerNumber);
        List<String> list = outboundMapper.selectBicycleList(outbound.getContainerNumber());
        String bicycles="";
        for (String bicycleId:list) {
            bicycles=bicycles+bicycleId+";";
        }
        outbound.setBicycleIds(bicycles);
        return outbound;
    }

    /**
     * 查询出库单列表
     *
     * @param outbound 出库单
     * @return 出库单
     */
    @Override
    public List<Outbound> selectOutboundList(Outbound outbound)
    {
        List<Outbound> outbounds = outboundMapper.selectOutboundList(outbound);
//        for (Outbound outbound1:outbounds) {
//            Consignment consignment = consignmentMapper.selectConsignmentByConsignmentId(outbound1.getConsignmentId());
//            Product product = productMapper.selectProductByProductId(consignment.getProductId());
//            outbound1.setTypes(consignment.getTypes());
//            outbound1.setClientOrderNumber(consignment.getClientOrderNumber());
//            outbound1.setDate(consignment.getDate());
//            outbound1.setClientName(consignment.getClientName());
//            outbound1.setProductName(consignment.getProductName());
//            outbound1.setColour(consignment.getColour());
//            outbound1.setWeekdaying(consignment.getWeekdaying());
//            outbound1.setProductId(consignment.getProductId());
//            outbound1.setQuantityShipped(consignment.getQuantityShipped());
//            outbound1.setSpecifications(product.getSpecifications());
//            outbound1.setCustomerModel(product.getCustomerModel());
//            outbound1.setWarehouse(consignment.getWarehouseId());
//        }
        return outbounds;
    }

    /**
     * 新增出库单
     *
     * @param outbound 出库单
     * @return 结果
     */
    @Override
    public int insertOutbound(Outbound outbound)
    {
        String[] split = outbound.getBicycleIds().split(";");
        String[] newArr;
        if (split.length!=1){
             newArr = Arrays.copyOfRange(split, 0, split.length - 1);
        }else {
             newArr=split;
        }
        outbound.setDelFlag(2L);
        outbound.setAudit(0L);
        outbound.setSynchState(0L);

        outbound.setWeekdayed((long) newArr.length);
        int i = outboundMapper.insertOutbound(outbound);
        for (String bicycleId:newArr) {
            String consignmentId = outbound.getConsignmentId();
            String containerNumber = outbound.getContainerNumber();
            outboundMapper.insertBicycle(consignmentId,containerNumber,bicycleId);
        }
        //回传已出库数量
        String ConsignmentId= outbound.getConsignmentId();
        int number=outboundMapper.selectNumberByConsignmentId(ConsignmentId);
        Consignment consignment = new Consignment();
        outbound.setColour(consignment.getColour());
        consignment.setConsignmentId(outbound.getConsignmentId());
        consignment.setQuantityShipped((long)number);
        consignmentMapper.updateConsignment(consignment);
        return i;
    }

    /**
     * 修改出库单
     *
     * @param outbound 出库单
     * @return 结果
     */
    @Override
    public int updateOutbound(Outbound outbound)
    {

        outboundMapper.deleteBicycleBycontainerNumber(outbound.getContainerNumber());
        String[] split = outbound.getBicycleIds().split(";");
        System.out.println(split[split.length-1]);
        String[] newArr;
        if (split.length!=1){
            newArr = Arrays.copyOfRange(split, 0, split.length - 1);
        }else {
            newArr=split;
        }
        for (String bicycleId:newArr) {
            String consignmentId = outbound.getConsignmentId();
            String containerNumber = outbound.getContainerNumber();
            outboundMapper.insertBicycle(consignmentId,containerNumber,bicycleId);
        }
        outbound.setWeekdayed((long)newArr.length);
        int number=outboundMapper.selectNumberByConsignmentId(outbound.getConsignmentId());
        outbound.setQuantityShipped((long)number);
        //回传已出库数量
        String ConsignmentId= outbound.getConsignmentId();
        int numbers=outboundMapper.selectNumberByConsignmentId(ConsignmentId);
        Consignment consignment = new Consignment();
        consignment.setQuantityShipped((long)numbers);
        consignment.setConsignmentId(outbound.getConsignmentId());
        consignmentMapper.updateConsignment(consignment);
        return outboundMapper.updateOutbound(outbound);
    }

    /**
     * 批量删除出库单
     *
     * @param consignmentIds 需要删除的出库单主键
     * @return 结果
     */
    @Override
    public int deleteOutboundByConsignmentIds(String[] consignmentIds)
    {
        return outboundMapper.deleteOutboundByConsignmentIds(consignmentIds);
    }

    /**
     * 删除出库单信息
     *
     * @param consignmentId 出库单主键
     * @return 结果
     */
    @Override
    public int deleteOutboundByConsignmentId(String consignmentId)
    {
        return outboundMapper.deleteOutboundByConsignmentId(consignmentId);
    }

    /**
     * 审核出库单
     * @param containerNumbers
     * @return
     */
    @Override
    public AjaxResult updateAudit(String[] containerNumbers) {
        for (int i = 0; i < containerNumbers.length; i++) {
            outboundMapper.updateAuditByoutboundService(containerNumbers[i]);
        }
        return AjaxResult.success();
    }
    /**
     * 反审核出库单
     * @param containerNumbers
     * @return
     */
    @Override
    public AjaxResult updateUnAudit(String[] containerNumbers) {
        for (int i = 0; i < containerNumbers.length; i++) {
            outboundMapper.updateUnAuditByoutboundService(containerNumbers[i]);
        }
        return AjaxResult.success();
    }
}
